package com.zoke.support.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.zoke.support.R;
import com.zoke.support.config.ZokeConfig;
import com.zoke.support.http.CallBackStores;
import com.zoke.support.http.ZokeCallback;
import com.zoke.support.http.ZokeParams;
import com.zoke.support.utils.MessageHandlerList;
import com.zoke.support.view.TitleManager;

/**
 * 支持MessageHandlerList的Fragment 统一管理网络请求
 * ps:从Fragment跳转到Activity，将Activity的主题设置为fragment_activity。
 * 
 * @author JackWu
 * 
 */
public class BaseFragment extends Fragment implements ZokeCallback,
		OnClickListener {
	private Handler mHandler;

	private LayoutInflater inflater;
	private View contentView;
	private ViewGroup container;
	protected TitleManager mtiManager;

	protected int mHashCode;

	protected Intent mIntent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 仅仅执行一次
		// 设置MessageHandlerList
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				handleMsg(msg);
			}
		};
		String className = this.getClass().getName();
		MessageHandlerList.addHandler(className, mHandler);
		mHashCode = this.hashCode();
		/** 添加回调 **/
		CallBackStores.getCallback().addCallback(mHashCode, this);
		mtiManager = new TitleManager(BaseFragment.this);
		mIntent = new Intent();
	}

	/** 切换UI 默认动画 **/
	protected void startUI(Intent intent) {
		// overridePendingTransition(enterAnim, exitAnim);
		startActivity(intent);
		getActivity().overridePendingTransition(R.anim.slide_in_from_left, R.anim.donot_move);
	}

	/** 动画方式切换UI **/
	protected void startUI(Intent intent, int enterAnim, int exitAnim) {
		getActivity().overridePendingTransition(enterAnim, exitAnim);
		startUI(intent);
	}
	

	/** 跳转界面ForResult **/
	protected void startUIForResult(Intent intent, int requestCode) {
		startActivityForResult(intent, requestCode);
		getActivity().overridePendingTransition(R.anim.slide_in_from_left, R.anim.donot_move);
	}

	/** title左按钮点击事件 **/
	public void onTitleClickLeft(View view) {

	}

	/** title标题右按钮点击事件 **/
	public void onTitleClickRight(View view) {
	}

	/** title标题点击事件 **/
	public void onTitleClick(View view) {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater = inflater;
		this.container = container;
		onCreateView(savedInstanceState);
		if (contentView == null)
			return super.onCreateView(inflater, container, savedInstanceState);
		return contentView;
	}

	/** 子类实现该方法 简化fragment添加View的方法 等同于fragment的onCreateView **/
	protected void onCreateView(Bundle savedInstanceState) {

	}

	/** 模仿Activity查找控件 **/
	public View findViewById(int id) {
		return contentView.findViewById(id);
	}

	/**
	 * 接收handle的消息 （ps:一定要调用super.handleMsg())
	 * 
	 * @param msg
	 */
	protected void handleMsg(Message msg) {
		switch (msg.what) {
		case ZokeConfig.ALL_UI_FINISH:
			break;
		default:
			break;
		}
	}

	@Override
	public void onDestroy() {
		// 移除消息池中对应的handler
		String className = this.getClass().getName();
		MessageHandlerList.removeHandler(className);
		/** 移除callBack **/
		CallBackStores.getCallback().deleteCallback(this.hashCode(), this);
		super.onDestroy();
		contentView = null;
		container = null;
		inflater = null;

	}

	@Override
	public void onSuccess(ZokeParams out) {

	}

	@Override
	public void onFails(ZokeParams out) {

	}

	/** 仿照Acitivity添加布局的方式 **/
	public void setContentView(int layoutResID) {
		setContentView(inflater.inflate(layoutResID, container, false));
	}

	public void setContentView(View view) {
		contentView = view;
	}

	public View getContentView() {
		return contentView;
	}

	@Override
	public void onClick(View view) {
		// 由于Fragment中不支持xml布局的onclick属性 解决方案
		if (view.getId() == R.id._title_container) {
			onTitleClick(view);
			return;
		}

		if (view.getId() == R.id._title_left_container) {
			onTitleClickLeft(view);
			return;
		}

		if (view.getId() == R.id._title_right_container) {
			onTitleClickRight(view);
			return;
		}

	}

}
